import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap();
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            map.put(nums[i], i);
        }
        for (int i = 0; i < n; i++) {
            if (map.containsKey(target - nums[i]) && map.get(target - nums[i]) != nums[i]) {
                return new int[]{i, map.get(target - nums[i])};
            }
        }
        return null;
    }

    //移动零
    //双指针移动
    public void moveZeroes(int[] nums) {
        int left = 0;
        int n = nums.length;
        if (n == 1) {
            return;
        }
        for (int i = 0; i < n; i++) {
            // i走到非零位置
            if (nums[i] == 0) {
                continue;
            }
            // left走到0位置
            while (left < n && nums[left] != 0) {
                left++;
            }
            // 交换
            if (left < i && nums[left] == 0) {
                int tmp = nums[i];
                nums[i] = nums[left];
                nums[left] = tmp;
            }

        }
    }

    //快慢指针
    public void moveZeroes1(int[] nums) {
        int show = 0;
        int fast = 0;
        int n = nums.length;
        while (fast < n) {
            //快指针遇到不是零的数字停下
            if(nums[fast] != 0) {
                int tmp = nums[show];
                nums[show] = nums[fast];
                nums[fast] = tmp;
                show++;
            }
            fast++;
        }
    }
}
